package com.rm.questions.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rm.questions.common.Result;
import com.rm.questions.entity.TrainPaper;
import com.rm.questions.service.ITrainPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/trainPaper")
public class TrainPaperController {

    @Autowired
    private ITrainPaperService ITrainPaperService;

    // 新增组卷
    @PostMapping
    public Result add(@RequestBody TrainPaper trainPaper) {
        boolean saved = ITrainPaperService.save(trainPaper);
        return saved ? Result.success("组卷添加成功") : Result.error("组卷添加失败");
    }

    // 根据ID删除组卷
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        boolean removed = ITrainPaperService.removeById(id);
        return removed ? Result.success("组卷删除成功") : Result.error("组卷删除失败");
    }

    // 更新组卷信息
    @PutMapping
    public Result update(@RequestBody TrainPaper trainPaper) {
        boolean updated = ITrainPaperService.updateById(trainPaper);
        return updated ? Result.success("组卷更新成功") : Result.error("组卷更新失败");
    }

    // 根据ID查询组卷
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        TrainPaper trainPaper = ITrainPaperService.getById(id);
        return trainPaper != null ? Result.success(trainPaper) : Result.error("未找到组卷");
    }

    // 分页查询组卷列表
    @GetMapping("/page")
    public Result page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        Page<TrainPaper> page = new Page<>(pageNum, pageSize);
        ITrainPaperService.page(page);
        return Result.success(page);
    }
}